package model.objects;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

import javax.swing.JPanel;

import model.general.Location;
import model.general.Util;
import model.move.IMove;
import model.move.MoveItems;
import model.observer.Obsever;
import model.option.IOption;
import model.option.Option;

public class Fish extends Sprites implements IAnimateItems, Obsever {
	private IOption option;
	private IAnimateSubActor hook;

	private boolean isCatch = false;
	private boolean isMove = true;
	private boolean isStandLeft;
	private boolean isChecking = true;
	private int speed = Util.DEFAULT_SPEED_MOVE;

	public Fish(Location location, String srcImgLeft, String srcImgRight,
			JPanel pnl, IOption option, IAnimateSubActor hook) {
		super(location, srcImgLeft, srcImgRight, pnl);
		this.move = new MoveItems(this.location);
		move.setSpeed(speed);
		this.hook = hook;
		this.option = option;
		// Obseverable o = (Obseverable) hook;
		// o.addObsever(this);
	}

	@Override
	public void check() {
		checkIsCatched();
		if (isCatched()) {
			catched();
		} else {
		}

	}

	public void checkIsCatched() {
		if (img != null) {
			Rectangle rect = getBounds();
			Rectangle recHook = hook.getBoundingBox();
			if (rect.intersects(recHook)) {
				isCatch = true;
			}
		} else {
			System.out.println("I'm not display");
		}
	}

	public void catched() {
		hook.catchedItems(this);
		isMove = false;
	}

	public boolean isCatched() {
		return isCatch;
	}

	@Override
	public void move() {
		if (isMove) {
			if (hasOutPanel()) {
				reset();
			} else {
				if (isStandLeft) {
					move.moveRight();
				} else {
					move.moveLeft();
				}

				// ngu mot khoang thoi gian nho nho =))
				try {
					Thread.sleep(6);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			move.stop();
		}

	}

	public boolean hasOutPanel() {
		int min = this.pnl.getX() - this.width - Util.LIMITED_LEFT_VIEW;
		int max = this.pnl.getX() + this.pnl.getWidth()
				+ Util.LIMITED_RIGHT_VIEW;

		if (location.getX() < min || location.getX() > max) {
			return true;
		}
		return false;
	}

	@Override
	public void show(Graphics g) {
		g.drawImage(img, location.getX(), location.getY(), null);
	}

	@Override
	public void updateLocation(Location loc) {
		this.location.setX(loc.getX());
		this.location.setY(loc.getY());
	}

	@Override
	public void destroy() {
		option.addOption();
		reset();
		isMove = true;
		isCatch = false;
	}

	public void reset() {
		Location loc = randLocation();
		this.location.setX(loc.getX());
		this.location.setY(loc.getY());
		randSpeed();
		findOrientation(location.getX());

	}

	private boolean findOrientation(int x) {
		if (x < (pnl.getX() + (pnl.getWidth() / 2))) {
			isStandLeft = true;
		} else {
			isStandLeft = false;
		}
		changeImage();
		return isStandLeft;
	}

	private void changeImage() {
		if (isStandLeft) {
			// img = loadImage.getbufferImage("fish1-right.png");
			img = loadImage.getbufferImage(srcImgRight);
		} else {
			// img = loadImage.getbufferImage("fish1-left.png");
			img = loadImage.getbufferImage(srcImgLeft);
		}
	}

	private void randSpeed() {
		Random rand = new Random();
		// System.out.println("speed now "+this.speed);
		if (speed - 1 > 0) {
			speed = rand.nextInt(speed - 1) + 1;
			setSpeed(speed);
		}
		// System.out.println("speed was change speed new "+this.speed);
	}

	private Location randLocation() {
		Random rand = new Random();
		double m = Math.random();
		int x;
		if (m <= 0.5) {
			x = this.pnl.getX() - this.width - Util.LIMITED_LEFT_VIEW; // 0 - 48
																		// = -48
		} else {
			x = this.pnl.getX() + this.pnl.getWidth() + Util.LIMITED_RIGHT_VIEW; // 0
																					// +
																					// 800
																					// =
																					// 800
																					// cong
																					// nhan
																					// mami
																					// nau
																					// ngon
																					// woa
																					// :))
		}
		int h = this.pnl.getHeight() / 2;
		int y = (int) (h * Math.random() + h) - this.height;
		return new Location(x, y);
	}

	@Override
	public Obsever getObserver() {
		return this;
	}

	@Override
	public void setMove(IMove move) {
		this.move = move;
	}

	@Override
	public void setSpeed(int steep) {
		this.speed = steep;
		this.move.setSpeed(steep);
	}

	@Override
	public void setMove(boolean ismove) {
		ismove = true;

	}

	@Override
	public Location getLocation() {
		return this.location;
	}

}
